package com.liull.netty.cust_protocol;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

/**
 * @Author: liull
 * @Description:
 * @Date: Created in 17:23 2020/11/15
 * @Modified By:
 */
public class PersonClientHandler extends SimpleChannelInboundHandler<PersonProtocol> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, PersonProtocol msg) throws Exception {
        System.out.println("收到服务端消息：" + msg);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        String msg = "LI YAN LIN, I LOVE YOU";
        PersonProtocol protocol = new PersonProtocol(msg.getBytes(CharsetUtil.UTF_8).length, msg.getBytes(CharsetUtil.UTF_8));
        ctx.writeAndFlush(protocol);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.channel().closeFuture().sync();
    }
}
